/**
 * Copyright (c) 时间, Tal.Ben 袁东昊 (ydonghao2@gmail.com).
 *
 * 版权
 */
package com.talben.soudian.fragment;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;
import org.kymjs.kjframe.KJDB;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;

import com.koushikdutta.ion.Ion;
import com.talben.soudian.AppContext;
import com.talben.soudian.TDevice;
import com.talben.soudian.adapter.ApkAdapter;
import com.talben.soudian.adapter.ListBaseAdapter;
import com.talben.soudian.api.SouDianAPI;
import com.talben.soudian.api.UrlConnect;
import com.talben.soudian.base.BaseListFragment;
import com.talben.soudian.bean.ApkDetail;
import com.talben.soudian.bean.ApkList;
import com.talben.soudian.ui.EmptyLayout;
import com.talben.soudian.ui.OnTabReselectListener;
import com.talben.soudian.ui.UIHelper;

/**
 * @Title: ApkFragment.java
 * @Package com.talben.soudian.fragment
 * @Description: apps列表
 * @author Tal.Ben
 * @date 2015年5月31日 下午3:22:59
 * @version v1.0
 */
public class GameFragment extends BaseListFragment<ApkDetail> implements
			OnTabReselectListener{

	private static final String CACHE_KEY_PREFIX = "apkslist_";
	public static final String BUNDLE_APK_TYPE = "BUNDLE_BLOG_TYPE";

	@Override
	public void onTabReselect() {
		onRefresh();
	}

	@Override
	protected ApkAdapter getListAdapter() {
		return new ApkAdapter(getActivity());
	}
	
	@Override
    protected String getCacheKeyPrefix() {
        return CACHE_KEY_PREFIX + mCatalog;
    }
	
	@Override
    protected ApkList parseList(JSONArray JArray) throws Exception {
		ApkList list = null;
        try {
            list = new ApkList();
            for (int i = 0; i < JArray.length(); i++) {
            	JSONObject jObj = JArray.getJSONObject(i);
            	ApkDetail apkDetail = new ApkDetail();
            	Timestamp ts = new Timestamp(jObj.getLong("timestamp"));  
                Date date = new Date();  
                date = ts;  
                apkDetail.setId(jObj.getInt("id"));
                apkDetail.setServerId(jObj.getString("id"));
            	apkDetail.setApkIcon(jObj.getString("icon"));
            	apkDetail.setPackageName(jObj.getString("packageName"));
            	apkDetail.setName(jObj.getString("name"));
            	apkDetail.setVersionCode(jObj.getString("versionCode"));
            	apkDetail.setVersionName(jObj.getString("versionName"));
            	apkDetail.setApkSize(jObj.getString("size"));
            	apkDetail.setCreateTime(date.toString());
            	apkDetail.setCoin(jObj.getString("gold"));
            	apkDetail.setDownloadUrl(jObj.getString("applink"));
            	apkDetail.setScreenshotsUrl(jObj.getString("pics"));
            	apkDetail.setDescription(jObj.getString("content"));
            	apkDetail.setScore(jObj.getString("score"));
            	apkDetail.setDownloadTimes(jObj.getString("downcount"));
        		if (TDevice.checkApkExist(getActivity(), apkDetail.getPackageName())) {
        			apkDetail.setBtnStatus("打开");
				}
        		list.getList().add(apkDetail);
			}
        } catch (NullPointerException e) {
            list = new ApkList();
        }
        return list;
    }
	
	@Override
    protected ApkList readList(Serializable seri) {
        return ((ApkList) seri);
    }
	
	@Override
    protected void sendRequestData() {
        SouDianAPI.getApkList(mCatalog, mCurrentPage, 10, mHandler);
    }
	
	@Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        ApkDetail apkDetail = mAdapter.getItem(position);
        if (apkDetail != null) {
//            UIHelper.showNewsRedirect(view.getContext(), news);
        }
    }
	
	@Override
    protected void executeOnLoadDataSuccess(List<ApkDetail> data) {
        if (mCatalog == ApkList.CATALOG_ALL
                || mCatalog == ApkList.CATALOG_RECOMMEND) {
            mErrorLayout.setErrorType(EmptyLayout.HIDE_LAYOUT);
            if (mState == STATE_REFRESH)
                mAdapter.clear();
            mAdapter.addData(data);
            mState = STATE_NOMORE;
            mAdapter.setState(ListBaseAdapter.STATE_NO_MORE);
            return;
        }
        super.executeOnLoadDataSuccess(data);
    }
	
	@Override
    public void onResume() {
        super.onResume();
        onRefresh();
    }
	
	@Override
    protected long getAutoRefreshTime() {
        // 最新apk两小时刷新一次
        if (mCatalog == ApkList.CATALOG_ALL) {
            return 2 * 60 * 60;
        }
        return super.getAutoRefreshTime();
    }
	
}

